package com.example.ui.share;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;

import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.net.AnalyzingJson;
import com.example.net.NetRequest;
import com.example.ui.learn.ListeningActivity;
import com.example.ui.leftmenu.Circle;
import com.example.ui.share.adapter.AroundCircleAdapter;
import com.example.util.Util;

public class CircleGroupActivity extends Activity {

	private AroundCircleAdapter item_adapter;
	private ArrayList<Circle> item6_c;
	private Button titleLeftBtn,titleRightBtn;
	private TextView titleText;
	private ListView listview;
    private String data;	
	private RadioGroup i6_rg;
	private String result;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.item06);		
		findView();				
		new Thread(item6_nearby).start();
	}
	private  OnClickListener i6_ocl=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.layout_title_left_btn:					
				finish();
				break;
			case R.id.layout_title_right_btn:
				Intent i1_l=new Intent();
				i1_l.setClass(CircleGroupActivity.this, CreateCircleGroupActivity.class);
				startActivity(i1_l);
				break;
			default:
				break;
			}
		}
	};
	
	
	private void findView() {
		titleLeftBtn = (Button)findViewById(R.id.layout_title_left_btn);
		titleRightBtn= (Button)findViewById(R.id.layout_title_right_btn);
		titleText=(TextView)findViewById(R.id.layout_title_middle_text);
		titleLeftBtn.setText("菜单");
		titleText.setText("学习圈");
		titleRightBtn.setText("创建");
		
		listview = (ListView)findViewById(R.id.i6_lv);
		i6_rg=(RadioGroup) findViewById(R.id.circle_tab_radio);   	  
		item6_c=new ArrayList<Circle>();
		item_adapter=new AroundCircleAdapter(this, item6_c);
		
		listview.setAdapter(item_adapter);
		titleLeftBtn.setOnClickListener(i6_ocl);
		titleRightBtn.setOnClickListener(i6_ocl);
		i6_rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup arg0, int rid) {
				switch (rid) {
				case R.id.radio_button0:// 首页

					 new Thread(item6_nearby).start(); 
					titleText.setText("学习圈");
					titleRightBtn.setVisibility(View.VISIBLE);
					titleRightBtn.setText("创建");
					break;
				case R.id.radio_button1:// 信息
					 new Thread(item6_my).start(); 
					
					titleText.setText("我的圈");
					titleRightBtn.setVisibility(View.INVISIBLE);
					break;
				case R.id.radio_button2:// 资料

					 new Thread(item6_hot).start(); 
					titleText.setText("热门圈");
					titleRightBtn.setVisibility(View.INVISIBLE);
					break;
				default:
						break;
				}
			}
		});
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long id) {
				
				Intent intent = new Intent(CircleGroupActivity.this,
						CircleHomeActivity.class);
				intent.putExtra("headImg", item6_c.get(position)
						.getThumb());
				intent.putExtra("titleName", item6_c.get(position)
						.getTitle());
				intent.putExtra("intro", item6_c.get(position)
						.getIntro());
				intent.putExtra("circleMemberNum", item6_c
						.get(position).getJoinNum());
				intent.putExtra("circleShareNum", item6_c.get(position)
						.getShareNum());
				intent.putExtra("circleId", item6_c.get(position)
						.getId());
				intent.putExtra("createCircleUserId", item6_c.get(position)
						.getUid());
				startActivity(intent);
				
				
			}
		});
		
	}
	Handler h=new Handler(){
		 public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 2:
				 item_adapter.setCcInfos(item6_c);
				 item_adapter.notifyDataSetChanged();
				break;
			case 1:
				Toast.makeText(CircleGroupActivity.this, "未找到数据", Toast.LENGTH_SHORT).show();
				break;

			default:
				break;
			}
			
		 };
		
	};
	Runnable item6_nearby = new Runnable() {

		@Override
		public void run() {			
			
			data = NetRequest.postRequestCircleAll();
			JSONObject jo;
			try {
				jo = new JSONObject(data);
		      result=jo.getString("response");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(result.equals("true")){
				item6_c = AnalyzingJson.getdata_Circle(data);
				h.sendEmptyMessage(2);
				
			}else{
				h.sendEmptyMessage(1);
			}
			
		}
	};
	Runnable item6_my = new Runnable(){  
		  
		@Override  
		public void run() {  
		    // TODO Auto-generated method stub  
		    data=NetRequest.postRequestCircleMy();
		    JSONObject jo;
			try {
				jo = new JSONObject(data);
		      result=jo.getString("response");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(result.equals("true")){
				item6_c=AnalyzingJson.getdata_Circle(data);
			    h.sendEmptyMessage(2);
				
			}else{
				h.sendEmptyMessage(1);
			}
		    
		}  
		  }; 
		  Runnable item6_hot = new Runnable(){  
			  
				@Override  
				public void run() {  
				    
				    data=NetRequest.postRequestCircleHot();
				    JSONObject jo;
					try {
						jo = new JSONObject(data);
				      result=jo.getString("response");
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if(result.equals("true")){
						 item6_c=AnalyzingJson.getdata_Circle(data);
						   h.sendEmptyMessage(2);
						
					}else{
						h.sendEmptyMessage(1);
					}
				   
				}  
				  }; 

	

	

}
